<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


class RedCross_Controller_Plugin_View extends Zend_Controller_Plugin_Abstract
{
    private $view;

    public function __construct(Zend_View $view)
    {
    	$this->view = $view;
    }

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        $baseUrl = $this->view->baseUrl();

//        $this->view->getHelper('serverUrl')->serverUrl()

        //Favicon
        $this->view->headLink()->headLink(array('rel' => 'shortcut icon', 'href' => $baseUrl . '/static/images/favicon.ico'), 'PREPEND');

        $path = APPLICATION_PATH . '/../public/static/';
        
        // Stylesheets
        if(file_exists($path . 'css/redcross.css')) {
            $this->view->headLink()->appendStylesheet($baseUrl . '/static/css/redcross.css', 'screen');
        } else {
            $this->view->headLink()->appendStylesheet($baseUrl . '/static/css/reset.css', 'screen');
            $this->view->headLink()->appendStylesheet($baseUrl . '/static/css/stylesheet.css', 'screen');

            $this->view->headLink()->appendStylesheet($baseUrl . '/static/css/jquery-ui-1.7.2.custom.css', 'screen');

            $this->view->headLink()->appendStylesheet($baseUrl . '/static/css/jquery.fancybox-1.3.1.css', 'screen');
            $this->view->headLink()->appendStylesheet($baseUrl . '/static/css/table_data.css', 'screen');

            $this->view->headLink()->appendStylesheet($baseUrl . '/static/css/datePicker.css', 'screen');
        }

        // JavaScript
        if(file_exists($path . 'javascript/javascript.js')) {
            $this->view->headScript()->appendFile($baseUrl . '/static/javascript/javascript.js');
        } else {
            $this->view->headScript()->appendFile($baseUrl . '/static/javascript/jquery.fancybox-1.3.1.pack.js');
            $this->view->headScript()->appendFile($baseUrl . '/static/javascript/jquery.dataTables.min.js');
            $this->view->headScript()->appendFile($baseUrl . '/static/javascript/dataTables.dateEuroSort.js');

            $this->view->headScript()->appendFile($baseUrl . '/static/javascript/redcross.js');
        }

        $inlineScript = 'var imgurl = "' . $this->view->imageLink('') . '";' . "\n";
        $inlineScript .= 'var baseurl = "' . $this->view->baseUrl() . '";' . "\n";
        $inlineScript .= 'var loadingAnimation = \'<img src="' . $this->view->imageLink('') . 'images/loader16x16.gif" />\';';
        $this->view->inlineScript(Zend_View_Helper_HeadScript::SCRIPT, $inlineScript);
    }

    public function postDispatch(Zend_Controller_Request_Abstract $request) {
        // Prepend page title
        $pageHeader = $this->view->pageHeader()->get();
        if(!empty($pageHeader)) {
            $this->view->headTitle()->prepend($pageHeader);
        }
    }
}
